<template>
  <el-dialog
    :title="mode === 'add' ? '新增页面' : '编辑页面'"
    :visible.sync="showDialog"
    width="600px"
    @close="closeEvent"
  >
    <el-form
      ref="form"
      label-width="80px"
      style="width: 500px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="组件名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="菜单顺序">
        <el-input />
      </el-form-item>
      <el-form-item label="菜单icon">
        <el-input />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  data() {
    return {
      mode: '',
      showDialog: false,
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', // string	非必须
        enVisible: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //
      },
      rules: {
        name: {
          required: true,
          message: '必填',
          trigget: 'change'
        }, //	string	非必须
        code: {
          required: true,
          message: '必填',
          trigget: 'change'
        }, //	string	非必须
        description: {
          required: true,
          message: '必填',
          trigget: 'change'
        }, // string	非必须
        enVisible: {
          required: true,
          message: '必填',
          trigget: 'change'
        } //	string	非必须
      }
    }
  },
  methods: {
    // 点击确定时
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
            this.$message.success('新增权限成功')
          } else if (this.mode === 'edit') {
            await sysPermissionPut(this.form)
            console.log(this.form)
            this.$message.success('编辑权限成功')
          }
          this.$emit('getData')
          this.showDialog = false
        }
      })
    },
    // 关闭弹窗时
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', // string	非必须
        enVisible: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //
      }
    }
  }
}
</script>

<style></style>
